/*     */ package com.yizhilu.os.image.util;
/*     */ 
/*     */

import com.google.gson.JsonObject;
import com.yizhilu.os.core.util.DateUtils;
import com.yizhilu.os.core.util.PropertyUtil;
import com.yizhilu.os.core.util.image.ImageHelper;
import it.sauronsoftware.jave.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/*     */ public class FileUtil
/*     */ {
/*  35 */   public static int CUS_PHOTO_WIDTH = 150;
/*  36 */   public static int CUS_PHOTO_HEIGHT = 150;
/*  37 */   private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
/*     */ 
/*  39 */   public static PropertyUtil propertyUtil = PropertyUtil.getInstance("project");
/*     */ 
/*  42 */   public static final String pathfix = propertyUtil.getProperty("file.pathfix");
/*     */ 
/*  44 */   public static final String rootpath = propertyUtil.getProperty("file.root");
/*     */   public static final String tempPath = "temp";
/*  48 */   public static final String webPath = propertyUtil.getProperty("webPath");
/*     */ 
/*     */   public static JsonObject saveImage(MultipartFile imgFile, String[] paths)
/*     */   {
/*  60 */     JsonObject obj = new JsonObject();
/*     */     try
/*     */     {
/*  63 */       String savePath = paths[0];
/*     */ 
/*  65 */       String urlPath = paths[1];
/*  66 */       String newFileName = "";
/*  67 */       String upFileName = imgFile.getOriginalFilename();
/*     */ 
/*  69 */       if (StringUtils.isNotEmpty(upFileName)) {
/*  70 */         if (!checkFileName(upFileName)) {
/*  71 */           obj.addProperty("error", Integer.valueOf(-1));
/*  72 */           obj.addProperty("message", "上传文件扩展名不允许。\n只允许 gif,jpg,jpeg,png,bmp格式。");
/*  73 */           return obj;
/*     */         }
/*     */ 
/*  76 */         if (imgFile.getSize() > 5242880L) {
/*  77 */           obj.addProperty("error", Integer.valueOf(-1));
/*  78 */           obj.addProperty("message", "上传文件大小不能超过5M");
/*  79 */           return obj;
/*     */         }
/*     */ 
/*  82 */         newFileName = getRandomFileNameString(upFileName);
/*     */ 
/*  84 */         File isD = new File(savePath);
/*     */ 
/*  86 */         if (!isD.isDirectory()) {
/*  87 */           isD.mkdirs();
/*     */         }
/*  89 */         if (!isD.exists()) {
/*  90 */           synchronized (FileUtil.class) {
/*  91 */             isD.mkdirs();
/*     */           }
/*     */         }
/*  94 */         String saveFilename = new StringBuilder().append(savePath).append(File.separator).append(newFileName).toString();
/*     */ 
/*  96 */         imgFile.transferTo(new File(saveFilename));
/*  97 */         obj.addProperty("error", Integer.valueOf(0));
/*  98 */         urlPath = new StringBuilder().append(urlPath).append("/").append(newFileName).toString();
/*  99 */         obj.addProperty("url", urlPath);
/*     */       } else {
/* 101 */         obj.addProperty("error", Integer.valueOf(-1));
/* 102 */         obj.addProperty("message", "文件名为空");
/*     */       }
/*     */ 
/* 105 */       return obj;
/*     */     }
/*     */     catch (Exception e) {
/* 108 */       logger.error("+++upload kindeditor images error", e);
/* 109 */       obj.addProperty("error", Integer.valueOf(-1));
/* 110 */       obj.addProperty("message", "上传异常，请稍后再试");
/* 111 */     }return obj;
/*     */   }
/*     */ 
/*     */   public static String getRandomFileNameString(String fileName)
/*     */   {
/* 125 */     StringBuffer buffer = new StringBuffer();
/*     */ 
/* 127 */     Lock lock = new ReentrantLock();
/* 128 */     lock.lock();
/*     */     try {
/* 130 */       buffer.append(System.currentTimeMillis());
/*     */ 
/* 132 */       buffer.append(getRandomString(6));
/*     */ 
/* 134 */       buffer.append(fileName.substring(fileName.lastIndexOf("."), fileName.length()));
/*     */     } finally {
/* 136 */       lock.unlock();
/*     */     }
/* 138 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public static String getRandomString(int len)
/*     */   {
/* 148 */     Random random = new Random();
/* 149 */     StringBuffer buffer = new StringBuffer();
/* 150 */     for (int i = 0; i < len; i++) {
/* 151 */       buffer.append(random.nextInt(10));
/* 152 */       random = new Random();
/*     */     }
/* 154 */     return buffer.toString();
/*     */   }
/*     */ 
/*     */   public static boolean checkFileName(String fileName)
/*     */   {
/* 166 */     HashMap extMap = new HashMap();
/* 167 */     extMap.put("image", "gif,jpg,jpeg,png,bmp,pdf,zip,rar");
/*     */ 
/* 175 */     String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
/* 176 */     if (!Arrays.asList(((String)extMap.get("image")).split(",")).contains(fileExt)) {
/* 177 */       return false;
/*     */     }
/* 179 */     return true;
/*     */   }
/*     */ 
/*     */   public static String[] getSavePathByRequest(HttpServletRequest request)
/*     */   {
/* 190 */     MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
/* 191 */     MultipartFile imgFile = multipartRequest.getFile("fileupload");
/* 192 */     String branchDir = "";
/* 193 */     if (imgFile != null) {
/* 194 */       String upFileName = imgFile.getOriginalFilename();
/* 195 */       if (upFileName.trim().endsWith(".pdf"))
/* 196 */         branchDir = "pdfFile";
/* 197 */       else if (upFileName.trim().endsWith(".swf"))
/* 198 */         branchDir = "swfFile";
/* 199 */       else if (upFileName.trim().endsWith(".zip"))
/* 200 */         branchDir = "zipFile";
/* 201 */       else if (upFileName.trim().endsWith(".rar"))
/* 202 */         branchDir = "rarFile";
/* 203 */       else if ((upFileName.trim().endsWith(".mp3")) || (upFileName.trim().endsWith(".MP3")))
/* 204 */         branchDir = "mp3File";
/* 205 */       else if ((upFileName.trim().endsWith(".mp4")) || (upFileName.trim().endsWith(".MP4"))) {
/* 206 */         branchDir = "mp4File";
/*     */       }
/*     */     }
/* 209 */     String base = request.getParameter("base");
/* 210 */     String param = request.getParameter("param");
/* 211 */     String dateStr = DateUtils.toString(new Date(), "yyyyMMdd");
/* 212 */     String ukey = request.getParameter("ukey");
/* 213 */     StringBuffer savePath = new StringBuffer();
/* 214 */     StringBuffer urlPath = new StringBuffer();
/* 215 */     if ((branchDir != null) && (branchDir.trim().length() > 0)) {
/* 216 */       savePath.append(rootpath).append(pathfix).append(File.separator).append(branchDir);
/* 217 */       urlPath.append("/").append(pathfix).append(File.separator).append(branchDir);
/*     */     } else {
/* 219 */       savePath.append(rootpath).append(pathfix);
/* 220 */       urlPath.append("/").append(pathfix);
/*     */     }
/* 222 */     if (StringUtils.isEmpty(base)) {
/* 223 */       base = "yizhilu";
/*     */     }
/* 225 */     savePath.append("/").append(base);
/* 226 */     urlPath.append("/").append(base);
/* 227 */     if (StringUtils.isEmpty(param)) {
/* 228 */       param = "common";
/*     */     }
/* 230 */     savePath.append("/").append(param);
/* 231 */     urlPath.append("/").append(param);
/*     */ 
/* 233 */     if (StringUtils.isNotEmpty(ukey)) {
/* 234 */       savePath.append("/").append(ukey);
/* 235 */       urlPath.append("/").append(ukey);
/*     */     }
/*     */ 
/* 238 */     savePath.append("/").append(dateStr);
/* 239 */     urlPath.append("/").append(dateStr);
/*     */ 
/* 241 */     String[] result = { savePath.toString(), urlPath.toString() };
/* 242 */     return result;
/*     */   }
/*     */
        public static String[] getSavePathByRequest(HttpServletRequest request,MultipartFile imgFile)
/*     */   {
/* 192 */     String branchDir = "";
/* 193 */     if (imgFile != null) {
/* 194 */       String upFileName = imgFile.getOriginalFilename();
/* 195 */       if (upFileName.trim().endsWith(".pdf"))
/* 196 */         branchDir = "pdfFile";
/* 197 */       else if (upFileName.trim().endsWith(".swf"))
/* 198 */         branchDir = "swfFile";
/* 199 */       else if (upFileName.trim().endsWith(".zip"))
/* 200 */         branchDir = "zipFile";
/* 201 */       else if (upFileName.trim().endsWith(".rar"))
/* 202 */         branchDir = "rarFile";
/* 203 */       else if ((upFileName.trim().endsWith(".mp3")) || (upFileName.trim().endsWith(".MP3")))
/* 204 */         branchDir = "mp3File";
/* 205 */       else if ((upFileName.trim().endsWith(".mp4")) || (upFileName.trim().endsWith(".MP4"))) {
/* 206 */         branchDir = "mp4File";
/*     */       } else if ((upFileName.trim().endsWith(".amr")) || (upFileName.trim().endsWith(".amr"))) {
/* 206 */         branchDir = "amr";
/*     */       }
/*     */     }
/* 209 */     String base = request.getParameter("base");
/* 210 */     String param = request.getParameter("param");
/* 211 */     String dateStr = DateUtils.toString(new Date(), "yyyyMMdd");
/* 212 */     String ukey = request.getParameter("ukey");
/* 213 */     StringBuffer savePath = new StringBuffer(rootpath);
/* 214 */     StringBuffer urlPath = new StringBuffer("/");
              StringBuilder path = new StringBuilder(pathfix);
/* 222 */     if (StringUtils.isEmpty(base)) {
/* 223 */       base = "yizhilu";
/*     */     }
/* 225 */     path.append(File.separator).append(base);
/* 227 */     if (StringUtils.isEmpty(param)) {
/* 228 */       param = "common";
/*     */     }
/* 230 */      path.append(File.separator).append(param);
/* 233 */     if (StringUtils.isNotEmpty(ukey)) {
/* 234 */       path.append(File.separator).append(ukey);
/*     */     }
/*     */   if (StringUtils.isNotBlank(branchDir)) {
/* 216 */       path.append(File.separator).append(branchDir);
/*     */     }
/* 238 */     path.append(File.separator).append(dateStr);
/*     */     savePath.append(path);
/*     */     urlPath.append(path);
/* 241 */     String[] result = { savePath.toString(), urlPath.toString() };
/* 242 */     return result;
/*     */   }
    /*     */
/*     */   public static String[] getTempSavePathByRequest(HttpServletRequest request)
/*     */   {
/* 253 */     String base = request.getParameter("base");
/* 254 */     String param = request.getParameter("param");
/* 255 */     String dateStr = DateUtils.toString(new Date(), "yyyyMMdd");
/*     */ 
/* 257 */     if (StringUtils.isEmpty(base)) {
/* 258 */       base = "yizhilu";
/*     */     }
/* 260 */     if (StringUtils.isEmpty(param)) {
/* 261 */       param = "common";
/*     */     }
/*     */ 
/* 264 */     String savePath = new StringBuilder().append(rootpath).append(pathfix).append("/").append("temp").append("/").append(base).append("/").append(param).append("/").append(dateStr).toString();
/*     */ 
/* 266 */     String urlPath = new StringBuilder().append("/").append(pathfix).append("/").append("temp").append("/").append(base).append("/").append(param).append("/").append(dateStr).toString();
/*     */ 
/* 268 */     String[] result = { savePath, urlPath };
/* 269 */     return result;
/*     */   }
/*     */ 
/*     */   public static JsonObject saveCutImage(String photoPath, int imageWidth, int imageHeight, int cutLeft, int cutTop, int dropWidth, int dropHeight)
/*     */   {
/* 288 */     JsonObject obj = new JsonObject();
/*     */ 
/* 290 */     Rectangle rec = createPhotoCutRec(imageWidth, imageHeight, cutLeft, cutTop, dropWidth, dropHeight);
/*     */ 
/* 292 */     File tempPic = new File(new StringBuilder().append(rootpath).append(photoPath).toString());
/*     */ 
/* 294 */     String newSavepath = photoPath.substring(0, photoPath.lastIndexOf("/")).replace("temp", "customer");
/* 295 */     File isD = new File(newSavepath);
/*     */ 
/* 297 */     if (!isD.isDirectory()) {
/* 298 */       isD.mkdirs();
/*     */     }
/* 300 */     if (!isD.exists()) {
/* 301 */       synchronized (FileUtil.class) {
/* 302 */         isD.mkdirs();
/*     */       }
/*     */     }
/*     */ 
/* 306 */     String newPhotoName = new StringBuilder().append(newSavepath).append("/").append(getRandomFileNameString(photoPath)).toString();
/* 307 */     File file = new File(new StringBuilder().append(rootpath).append(newPhotoName).toString());
/*     */     try
/*     */     {
/* 310 */       saveSubImage(tempPic, file, rec, new int[] { imageWidth, imageHeight, cutLeft, cutTop, dropWidth, dropHeight });
/*     */ 
/* 312 */       obj.addProperty("url", newPhotoName.replace(rootpath, "/"));
/* 313 */       obj.addProperty("error", Integer.valueOf(0));
/*     */     } catch (IOException e) {
/* 315 */       e.printStackTrace();
/* 316 */       obj.addProperty("error", Integer.valueOf(-1));
/* 317 */       obj.addProperty("message", "上传文件大小不能超过5M");
/*     */     }
/*     */ 
/* 320 */     return obj;
/*     */   }
/*     */ 
/*     */   private static Rectangle createPhotoCutRec(int imageWidth, int imageHeight, int cutLeft, int cutTop, int dropWidth, int dropHeight) {
/* 324 */     int recX = cutLeft > 0 ? cutLeft : 0;
/* 325 */     int recY = cutTop > 0 ? cutTop : 0;
/* 326 */     int recWidth = dropWidth;
/* 327 */     int recHieght = dropHeight;
/* 328 */     if (cutLeft < 0)
/*     */     {
/* 330 */       if (imageWidth - cutLeft > dropWidth)
/* 331 */         recWidth = dropWidth + cutLeft;
/*     */       else {
/* 333 */         recWidth = imageWidth;
/*     */       }
/*     */     }
/* 336 */     else if (imageWidth - cutLeft < dropWidth) {
/* 337 */       recWidth = imageWidth - cutLeft;
/*     */     }
/*     */ 
/* 341 */     if (cutTop < 0)
/*     */     {
/* 343 */       if (imageHeight - cutTop > dropHeight)
/* 344 */         recHieght = dropHeight + cutTop;
/*     */       else {
/* 346 */         recHieght = imageHeight;
/*     */       }
/*     */ 
/*     */     }
/* 350 */     else if (imageHeight - cutTop < dropHeight) {
/* 351 */       recHieght = imageHeight - cutTop;
/*     */     }
/*     */ 
/* 354 */     return new Rectangle(recX, recY, recWidth, recHieght);
/*     */   }
/*     */ 
/*     */   private static void saveSubImage(File srcImageFile, File descDir, Rectangle rect, int[] intParms) throws IOException {
/* 358 */     ImageHelper.cut(srcImageFile, descDir, rect, intParms);
/*     */   }
/*     */ 
/*     */   public static String getFileContent(String path)
/*     */   {
/* 368 */     File myFile = new File(path);
/* 369 */     if (!myFile.exists()) {
/* 370 */       System.err.println(new StringBuilder().append("Can't Find ").append(path).toString());
/*     */     }
/* 372 */     BufferedReader in = null;
/* 373 */     StringBuilder sb = new StringBuilder();
/*     */     try {
/* 375 */       InputStreamReader read = new InputStreamReader(new FileInputStream(myFile), "UTF-8");
/* 376 */       in = new BufferedReader(read);
/*     */       String str;
/* 378 */       while ((str = in.readLine()) != null)
/* 379 */         sb.append(str);
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 383 */       e.getStackTrace();
/*     */     } finally {
/* 385 */       if (in != null) {
/*     */         try {
/* 387 */           in.close();
/*     */         } catch (IOException e) {
/* 389 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     }
/* 393 */     return sb.toString();
/*     */   }
/*     */ 
/*     */   public static File writeFileContent(String content, String path)
/*     */   {
/* 404 */     File file = new File(path);
/* 405 */     FileOutputStream fileout = null;
/*     */     try {
/* 407 */       if (!file.exists()) {
/* 408 */         file.createNewFile();
/*     */       }
/* 410 */       fileout = new FileOutputStream(file);
/* 411 */       fileout.write(content.getBytes("utf-8"));
/* 412 */       fileout.flush();
/*     */     }
/*     */     catch (Exception e) {
/* 415 */       e.printStackTrace();
/*     */     } finally {
/* 417 */       if (fileout != null) {
/*     */         try {
/* 419 */           fileout.close();
/*     */         } catch (IOException e) {
/* 421 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     }
/* 425 */     return file;
/*     */   }
/*     */ 
/*     */   public static boolean deleteImageFile(String filePath)
/*     */   {
/* 435 */     String realpath = new StringBuilder().append(rootpath).append(filePath).toString();
/* 436 */     realpath.replaceAll("//", "/");
/* 437 */     File file = new File(realpath);
/*     */ 
/* 439 */     if (checkFileName(filePath)) {
/* 440 */       System.out.println("a is delete file...................");
/* 441 */       if (file.exists()) {
/* 442 */         file.delete();
/*     */       }
/* 444 */       return true;
/*     */     }
/* 446 */     System.out.println("not delete file...................");
/* 447 */     return false;
/*     */   }
/*     */ 
/*     */   public static boolean deleteSwfImage(String filePath)
/*     */   {
/* 458 */     if (!filePath.endsWith(".swf")) {
/* 459 */       return false;
/*     */     }
/* 461 */     String realpath = new StringBuilder().append(rootpath).append(filePath).toString();
/* 462 */     realpath = realpath.replaceAll("//", "/");
/* 463 */     File file = new File(realpath);
/* 464 */     int index = 0;
/* 465 */     if (file.exists()) {
/* 466 */       file.delete();
/* 467 */       index++;
/*     */     } else {
/* 469 */       return false;
/*     */     }
/* 471 */     realpath = realpath.replace(".swf", ".pdf");
/* 472 */     file = new File(realpath);
/* 473 */     if (file.exists()) {
/* 474 */       file.delete();
/* 475 */       index++;
/*     */     } else {
/* 477 */       return false;
/*     */     }
/* 479 */     String[] arr = realpath.split("\\.");
/* 480 */     realpath = arr[0];
/* 481 */     file = new File(realpath);
/* 482 */     if (file.exists()) {
/* 483 */       deleteFile(file);
/* 484 */       index++;
/*     */     } else {
/* 486 */       return false;
/*     */     }
/* 488 */     if (index == 3) {
/* 489 */       return true;
/*     */     }
/* 491 */     return false;
/*     */   }
/*     */ 
/*     */   private static void deleteFile(File file)
/*     */   {
/* 499 */     if (file.exists()) {
/* 500 */       if (file.isFile()) {
/* 501 */         file.delete();
/* 502 */       } else if (file.isDirectory()) {
/* 503 */         File[] files = file.listFiles();
/* 504 */         for (int i = 0; i < files.length; i++) {
/* 505 */           deleteFile(files[i]);
/*     */         }
/*     */       }
/* 508 */       file.delete();
/*     */     } else {
/* 510 */       System.out.println("所删除的文件不存在！\n");
/*     */     }
/*     */   }
/*     */ 
/*     */   public static JsonObject saveFile(String urlString, String savepath)
/*     */   {
/* 521 */     JsonObject obj = new JsonObject();
/*     */ 
/* 523 */     File isD = new File(savepath);
/*     */ 
/* 525 */     if (!isD.isDirectory()) {
/* 526 */       isD.mkdirs();
/*     */     }
/* 528 */     if (!isD.exists()) {
/* 529 */       synchronized (FileUtil.class) {
/* 530 */         isD.mkdirs();
/*     */       }
/*     */     }
/*     */ 
/* 534 */     String newFileName = getRandomFileNameString("a.jpg");
/* 535 */     String saveFilename = new StringBuilder().append(savepath).append("/").append(newFileName).toString();
/*     */     try
/*     */     {
/* 538 */       URL url = new URL(urlString);
/*     */ 
/* 540 */       HttpURLConnection conn = (HttpURLConnection)url.openConnection();
/*     */ 
/* 542 */       conn.setRequestMethod("GET");
/*     */ 
/* 544 */       conn.setConnectTimeout(5000);
/*     */ 
/* 546 */       InputStream inStream = conn.getInputStream();
/*     */ 
/* 548 */       byte[] data = readInputStream(inStream);
/*     */ 
/* 550 */       File imageFile = new File(saveFilename);
/*     */ 
/* 552 */       FileOutputStream outStream = new FileOutputStream(imageFile);
/*     */ 
/* 554 */       outStream.write(data);
/*     */ 
/* 556 */       outStream.close();
/*     */     } catch (Exception e) {
/* 558 */       e.printStackTrace();
/*     */     }
/* 560 */     obj.addProperty("url", saveFilename.replace(rootpath, "/"));
/* 561 */     return obj;
/*     */   }
/*     */ 
/*     */   public static byte[] readInputStream(InputStream inStream) throws Exception
/*     */   {
/* 566 */     ByteArrayOutputStream outStream = new ByteArrayOutputStream();
/*     */ 
/* 568 */     byte[] buffer = new byte[1024];
/*     */ 
/* 570 */     int len = 0;
/*     */ 
/* 572 */     while ((len = inStream.read(buffer)) != -1)
/*     */     {
/* 574 */       outStream.write(buffer, 0, len);
/*     */     }
/*     */ 
/* 577 */     inStream.close();
/*     */ 
/* 579 */     return outStream.toByteArray();
/*     */   }
/*     */ 
/*     */   public static JsonObject saveCommonFile(MultipartFile file, String[] paths)
/*     */   {
/* 594 */     JsonObject obj = new JsonObject();
/*     */     try
/*     */     {
/* 597 */       String savePath = paths[0];
/*     */ 
/* 599 */       String urlPath = paths[1];
/* 600 */       String newFileName = "";
/* 601 */       String upFileName = file.getOriginalFilename();
/* 602 */       if (StringUtils.isNotEmpty(upFileName))
/*     */       {
/* 604 */         newFileName = getRandomFileNameString(upFileName);
/*     */ 
/* 606 */         File isD = new File(savePath);
/*     */ 
/* 608 */         if (!isD.isDirectory()) {
/* 609 */           isD.mkdirs();
/*     */         }
/* 611 */         if (!isD.exists()) {
/* 612 */           synchronized (FileUtil.class) {
/* 613 */             isD.mkdirs();
/*     */           }
/*     */         }
/* 616 */         String saveFilename = new StringBuilder().append(savePath).append(File.separator).append(newFileName).toString();
/*     */ 
/* 618 */         file.transferTo(new File(saveFilename));
                  if(urlPath.indexOf("amr")>-1){
                      String mp3Filename = saveFilename.replaceAll("amr","mp3");
                      changeToMp3(saveFilename,mp3Filename);
                      urlPath = urlPath.replace("amr","mp3");
                      newFileName = newFileName.replace("amr","mp3");
                  }
/* 619 */         obj.addProperty("error", Integer.valueOf(0));
/* 620 */         urlPath = new StringBuilder().append(urlPath).append("/").append(newFileName).toString();
/* 621 */         obj.addProperty("url", urlPath);
/*     */       } else {
/* 623 */         obj.addProperty("error", Integer.valueOf(-1));
/* 624 */         obj.addProperty("message", "文件名为空");
/*     */       }
/*     */ 
/* 627 */       return obj;
/*     */     }
/*     */     catch (Exception e) {
/* 630 */       logger.error("+++upload kindeditor images error", e);
/* 631 */       obj.addProperty("error", Integer.valueOf(-1));
/* 632 */       obj.addProperty("message", "上传异常，请稍后再试");
/* 633 */     }return obj;
/*     */   }
/*     */   private static final String[] audioExt = new String[]{"mp3"};
    private static final Integer AUDIO_MAX_SIZE = 1024*1024*100;
    public static JsonObject saveAudio(MultipartFile imgFile, String[] paths) {
        JsonObject obj = new JsonObject();
        try {
            String savePath = paths[0];
            String urlPath = paths[1];
            String newFileName = "";
            String upFileName = imgFile.getOriginalFilename();
            if (StringUtils.isNotEmpty(upFileName)) {
                if (!checkAudioType(upFileName)) {
                    obj.addProperty("error", Integer.valueOf(-1));
                    obj.addProperty("message", "上传文件扩展名不允许。\n只允许"+StringUtils.join(audioExt,",")+"格式。");
                    return obj;
                }
                if (imgFile.getSize() > AUDIO_MAX_SIZE) {
                    obj.addProperty("error", Integer.valueOf(-1));
                    obj.addProperty("message", "上传文件大小不能超过"+AUDIO_MAX_SIZE/1024/1024+"M");
                    return obj;
                }
                newFileName = getRandomFileNameString(upFileName);
                File isD = new File(savePath);
                if (!isD.isDirectory()) {
                    isD.mkdirs();
                }
                if (!isD.exists()) {
                    synchronized (FileUtil.class) {
                        isD.mkdirs();
                    }
                }
                String saveFilename = new StringBuilder().append(savePath).append(File.separator).append(newFileName).toString();
                imgFile.transferTo(new File(saveFilename));
                obj.addProperty("error", Integer.valueOf(0));
                urlPath = new StringBuilder().append(urlPath).append("/").append(newFileName).toString();
                obj.addProperty("url", urlPath);
            } else {
                obj.addProperty("error", Integer.valueOf(-1));
                obj.addProperty("message", "文件名为空");
            }
            return obj;
        } catch (Exception e) {
            logger.error("audio upload images error", e);
            obj.addProperty("error", Integer.valueOf(-1));
            obj.addProperty("message", "上传异常，请稍后再试");
        }
        return obj;
    }
    public static boolean checkAudioType(String fileName) {
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        if (!Arrays.asList(audioExt).contains(fileExt)) {
            return false;
        }
        return true;
    }
/*     */   public static File doZip(String sourceDir, String zipFilePath)
/*     */     throws IOException
/*     */   {
/* 641 */     File file = new File(sourceDir);
/* 642 */     File zipFile = new File(zipFilePath);
/* 643 */     if ((zipFile.exists()) && (file.exists())) {
/* 644 */       long fileTime = file.lastModified();
/* 645 */       long zipFileTime = zipFile.lastModified();
/*     */ 
/* 647 */       if (zipFileTime >= fileTime) {
/* 648 */         return zipFile;
/*     */       }
/*     */     }
/* 651 */     ZipOutputStream zos = null;
/*     */     try
/*     */     {
/* 654 */       OutputStream os = new FileOutputStream(zipFile);
/* 655 */       BufferedOutputStream bos = new BufferedOutputStream(os);
/* 656 */       zos = new ZipOutputStream(bos);
/*     */ 
/* 658 */       String basePath = null;
/*     */ 
/* 661 */       if (file.isDirectory())
/* 662 */         basePath = file.getPath();
/*     */       else {
/* 664 */         basePath = file.getParent();
/*     */       }
/*     */ 
/* 667 */       zipFile(file, basePath, zos);
/*     */     } finally {
/* 669 */       if (zos != null) {
/* 670 */         zos.closeEntry();
/* 671 */         zos.close();
/*     */       }
/*     */     }
/*     */ 
/* 675 */     return zipFile;
/*     */   }
/*     */ 
/*     */   private static void zipFile(File source, String basePath, ZipOutputStream zos)
/*     */     throws IOException
/*     */   {
/* 685 */     File[] files = null;
/* 686 */     if (source.isDirectory()) {
/* 687 */       files = source.listFiles();
/*     */     } else {
/* 689 */       files = new File[1];
/* 690 */       files[0] = source;
/*     */     }
/*     */ 
/* 693 */     InputStream is = null;
/*     */ 
/* 695 */     byte[] buf = new byte[1024];
/* 696 */     int length = 0;
/*     */     try {
/* 698 */       for (File file : files)
/* 699 */         if (file.isDirectory()) {
/* 700 */           String pathName = new StringBuilder().append(file.getPath().substring(basePath.length() + 1)).append("/").toString();
/* 701 */           zos.putNextEntry(new ZipEntry(pathName));
/* 702 */           zipFile(file, basePath, zos);
/*     */         } else {
/* 704 */           String pathName = file.getPath().substring(basePath.length() + 1);
/* 705 */           is = new FileInputStream(file);
/* 706 */           BufferedInputStream bis = new BufferedInputStream(is);
/* 707 */           zos.putNextEntry(new ZipEntry(pathName));
/* 708 */           while ((length = bis.read(buf)) > 0) {
/* 709 */             zos.write(buf, 0, length);
/*     */           }
/* 711 */           bis.close();
/*     */         }
/*     */     }
/*     */     finally {
/* 715 */       if (is != null)
/* 716 */         is.close();
/*     */     }
/*     */   }
            public static void changeToMp3(String sourcePath, String targetPath) {
                File source = new File(sourcePath);
                File target = new File(targetPath);
                AudioAttributes audio = new AudioAttributes();
                Encoder encoder = new Encoder(new MyFFMPEGExecute());
                audio.setCodec("libmp3lame");
                EncodingAttributes attrs = new EncodingAttributes();
                attrs.setFormat("mp3");
                attrs.setAudioAttributes(audio);
                try {
                    encoder.encode(source, target, attrs);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InputFormatException e) {
                    e.printStackTrace();
                } catch (EncoderException e) {
                    if(!"  Duration: N/A, bitrate: N/A".equals(e.getMessage()))
                    e.printStackTrace();
                }
            }
   }
